package com.zxh.dubbozxh.framework;

import com.zxh.dubbozxh.framework.protocol.http.HttpProtocol;
import com.zxh.dubbozxh.framework.register.RemoteMapRegister;
import java.lang.reflect.Proxy;


public class ProxyFactory {

    public static <T> T getProxy(Class interfaceClass) {

        Object proxyInstance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, (proxy, method, args) -> {
            Invocation invocation = new Invocation
                    (interfaceClass.getName(),method.getName(),method.getParameterTypes(),args);

            Protocol protocol = ProtocolFactory.getProtocol();

            // 负载均衡
            URL url = LoadBalance.random(RemoteMapRegister.get(interfaceClass.getName()));

            return protocol.send(url,invocation);
        });

        return (T)proxyInstance;

    }

}
